package main

import (
	"encoding/json"
	//	"errors"
	"io/ioutil"
	"net/http"

	"github.com/whxmail/whx/types"

	//	"github.com/emersion/go-imap"
	//	"github.com/emersion/go-imap/client"
)

func login(w http.ResponseWriter, r *http.Request) {
	//error controler
	errs := types.NewErrorControler()

	buf, err := ioutil.ReadAll(r.Body)
	errs.Check(err)

	var data types.Data
	err = json.Unmarshal(buf, &data)
	errs.Check(err)

	username, err := data.GetUserName()
	errs.Check(err)
	password, err := data.GetPassWord()
	errs.Check(err)
	//test(err)
	test(errs.Error())

	test(username)
	test(password)
	dt := types.Data{"success": "bye", "ERROR": errs.Errs()}
	test(dt)

	var b []byte
	b, _ = json.Marshal(dt)
	test(string(b))
	var d types.Data
	json.Unmarshal(b, &d)
	test(d)

	var n int
	n, _ = w.Write(b)
	test(n)

}

func main() {
	mux := types.NewMux()
	mux.Post("/login", login)
	//	http.HandleFunc("/login", login)
	http.ListenAndServe(":9090", mux)
	/*This is tcp server
	log.Println("Connecting to server...")
	server := types.NewTCPServer()

	//Register router
	server.MuxRegister("LOGIN", login)
	server.ListenAndServer("127.0.1.1:9999", nil)
	*/

	/*
		if cmd[0] == "LOGIN" {
			mail := cmd[1]
			pwd := cmd[2]

			// Connect to imapserver
			test("Connect to IMAPServer")
			c, err := client.DialTLS("imap.qq.com:993", nil)
			if err != nil {
				log.Fatal(err)
			}
			log.Println("Connected")

			// Don't forget to logout
			defer c.Logout()

			//Obtain information about user

			// Login

			if err := c.Login(mail, pwd); err != nil {
				log.Fatal(err)
			}
			log.Println("Logged in")

			// List mailboxes
			mailboxes := make(chan *imap.MailboxInfo)
			done := make(chan error, 1)
			go func() {
				done <- c.List("", "*", mailboxes)
			}()

			log.Println("Mailboxes:")
			for m := range mailboxes {
				log.Println("* " + m.Name)
			}

			if err := <-done; err != nil {
				log.Fatal(err)
			}

			// Select INBOX
			mbox, err := c.Select("INBOX", false)
			if err != nil {
				log.Fatal(err)
			}
			log.Println("Flags for INBOX:", mbox.Flags)

			// Get the last 4 messages
			seqset, _ := imap.NewSeqSet("")
			seqset.AddRange(mbox.Messages-3, mbox.Messages)

			messages := make(chan *imap.Message)
			done = make(chan error, 1)
			go func() {
				done <- c.Fetch(seqset, []string{imap.EnvelopeMsgAttr}, messages)
			}()

			for msg := range messages {
				log.Println(msg.Envelope.Subject)
			}

			if err := <-done; err != nil {
				log.Fatal(err)
			}

			log.Println("Done!")
		}
	*/
}
